import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { environment } from 'src/environments/environment';
import { __param } from 'tslib';
@Injectable({
  providedIn: 'root'
})
export class JobOrderService {

  constructor(private http:HttpClient) { }
  private jobOrder = '/JobOrder';
  CreateJobOrder(data:any){
    const url = this.jobOrder+'/CreateJobOrder';
    return this.http.post(environment.apiUrl+ url,data);
  }

  GetJobOrders(PartNum?:string,PartName?:string,JobOrderNum?:string,JobOrderStatus?:string,JobOrderType?:string, JobOrderProcessStatus?:string,StartTime?:string,EndTime?:string,page?: number, pageSize?: number){
    let params = new HttpParams();
    if (PartNum ) {
      params = params.set('PartNum', PartNum);
    }
    if (PartName ) {
      params = params.set('PartName', PartName);
    }
    if (JobOrderNum ) {
      params = params.set('JobOrderNum', JobOrderNum);
    }
    if (JobOrderStatus ) {
      params = params.set('JobOrderStatus', JobOrderStatus);
    }
    if (JobOrderType ) {
      params = params.set('JobOrderType', JobOrderType);
    }
    if(JobOrderProcessStatus){
      params = params.set('JobOrderProcessStatus', JobOrderProcessStatus);
    }
    if (StartTime ) {
      params = params.set('StartTime', StartTime);
    }
    if (EndTime ) {
      params = params.set('EndTime', EndTime);
    }
    if (page !== undefined && pageSize !== undefined) {
      params = params.set('PageNumber', page.toString());
      params = params.set('PageSize', pageSize.toString());
    }
    return this.http.get(environment.apiUrl+ this.jobOrder+'/GetJobOrders',{
      params: params
    })
  }
  GetJobOrderById(id:number){
    const url = this.jobOrder+'/GetJobOrderById';
    return this.http.get(environment.apiUrl+ url,{params:{id}});
  }

  UpdateJobOrder(jobOrderId:number,data:any){
    const url = this.jobOrder+'/UpdateJobOrder';
    return this.http.put(environment.apiUrl+ url,data,{params:{jobOrderId}});
  }
  DeleteJobOrders(ids:any){
    const url = this.jobOrder+'/DeleteJobOrders';
    const options = {
      body: ids
    };
    return this.http.delete(environment.apiUrl+ url,options);
  }
  ApprovalJobOrder(id:any){
    const url = this.jobOrder+'/ApprovalJobOrder';
    let params = new HttpParams().set('jobOrderId', id.toString());
    return this.http.put(environment.apiUrl+ url, null, {params: params});
  }
  UnApprovalJobOrder(id:any){
    const url = this.jobOrder+'/UnApprovalJobOrder';
    let params = new HttpParams().set('jobOrderId', id.toString());
    return this.http.put(environment.apiUrl+ url, null, {params: params});
  }

  GetAllLogs(){
    const url = this.jobOrder + '/GetAllJobOrderLogs';
    return this.http.get(environment.apiUrl+ url,{params:{
      PageNumber:1,
      PageSize:9999
    }});
  }

  GetLogsByJobOrderId(JobOrderId:any){
    const url = this.jobOrder + '/GetLogsByJobOrderId';
    return this.http.get(environment.apiUrl+ url,{params:{id:JobOrderId,PageNumber:1,PageSize:9999}});
  }
  AssignProcessesToUser(data:any){
    const url = this.jobOrder + '/AssignProcessesToUser';
    return this.http.post(environment.apiUrl+ url,data);
  }
  RevokeAssignUserFromProcess(jobOrderProcessID: any, data: any) {
    const url = this.jobOrder + '/RevokeAssignUserFromProcess';
    let params = new HttpParams().set('jobOrderProcessID', jobOrderProcessID);
    let headers = new HttpHeaders().set('Content-Type', 'application/json');
    return this.http.put(environment.apiUrl + url, data, { params: params, headers: headers });
  }
  RevokeAssignProcessesToUser(data:any){
    const url = this.jobOrder + '/RevokeAssignProcessesToUser';
    return this.http.post(environment.apiUrl+ url,data);
  }

  GetJobOrdersProcess(JobOrderProcessStatus?:string, page?: number, pageSize?: number){
    let params = new HttpParams();
    if (JobOrderProcessStatus ) {
      params = params.set('JobOrderProcessStatus', JobOrderProcessStatus);
    }
    if (page !== undefined && pageSize !== undefined) {
      params = params.set('PageNumber', page.toString());
      params = params.set('PageSize', pageSize.toString());
    }
    return this.http.get(environment.apiUrl+ this.jobOrder+'/GetJobOrdersProcess',{
      params: params
    })
  }

  StartProcess(jobOrderProcessId:any){
    const url = this.jobOrder + '/StartProcess';
    let params = new HttpParams().set('jobOrderProcessId', jobOrderProcessId);
    return this.http.post(environment.apiUrl+ url,null,{params: params});
  }
  ReportProcess(data:any){
    const url = this.jobOrder + '/ReportProcess';
    return this.http.post(environment.apiUrl+ url,data);
  }

  SuspendProcess(jobOrderProcessId:any,reason:any,note:any){
    const url = this.jobOrder + '/SuspendProcess';
    let params = new HttpParams().set('jobOrderProcessId', jobOrderProcessId).set('reason', reason).set('note', note);
    return this.http.post(environment.apiUrl+ url,null,{params: params});
  }
  GetAllIssueJobOrders(page?: number, pageSize?: number){
    let params = new HttpParams();
    if (page !== undefined && pageSize !== undefined) {
      params = params.set('pageNumber', page.toString());
      params = params.set('pageSize', pageSize.toString());
    }
    return this.http.get(environment.apiUrl+ '/Warehouse/GetAllIssueJobOrders',{
      params: params
    })
  }
  ContinueProcess(jobOrderProcessId:any){
    const url = this.jobOrder + '/ContinueProcess';
    let params = new HttpParams().set('jobOrderProcessId', jobOrderProcessId);
    return this.http.post(environment.apiUrl+ url,null,{params: params});
  }
  GetJobOrderProcess(JobOrderNum:any,ProcessNum:any){
    const url = this.jobOrder + '/GetJobOrdersProcess';
    return this.http.get(environment.apiUrl+ url,{params:{JobOrderNum,ProcessNum}});
  }

  OverReportProcess(data:any){
    const url = this.jobOrder + '/OverReportProcess';
    return this.http.post(environment.apiUrl+ url,data);
  }
  GetProductionWarehousingPaged(ProductionWarehousingNum?:string,StartTime?:string,EndTime?:string, page?: number, pageSize?: number){
    let params = new HttpParams();
    if (ProductionWarehousingNum ) {
      params = params.set('ProductionWarehousingNum', ProductionWarehousingNum);
    }
    if (StartTime ) {
      params = params.set('StartTime', StartTime);
    }
    if (EndTime ) {
      params = params.set('EndTime', EndTime);
    }
    if (page !== undefined && pageSize !== undefined) {
      params = params.set('PageNumber', page.toString());
      params = params.set('PageSize', pageSize.toString());
    }
    return this.http.get(environment.apiUrl+ '/JobOrder/GetProductionWarehousingPaged',{
      params: params
    })
  }
  ProductionWarehousing(data:any){
    const url = this.jobOrder + '/ProductionWarehousing';
    return this.http.post(environment.apiUrl+ url,data);
  }


  GetJobOrdersProcess2( PartNum?: string, PartName?: string,JobOrderNum?:string,JobOrderProcessStatus?:string, ProcessNum?:string,ProcessName?:string,pageNumber?: number, pageSize?: number,weiwai?:any,IsWithPart?:any){
    let params = new HttpParams();
    if (PartNum ) {
      params = params.set('PartNum', PartNum);
    }
    if (PartName ) {
      params = params.set('PartName', PartName);
    }
    if (JobOrderNum ) {
      params = params.set('JobOrderNum', JobOrderNum);
    }
    if (JobOrderProcessStatus ) {
      params = params.set('JobOrderProcessStatus', JobOrderProcessStatus);
    }
    if (ProcessNum ) {
      params = params.set('ProcessNum', ProcessNum);
    }
    if (ProcessName ) {
      params = params.set('ProcessName', ProcessName);
    }
    if (pageNumber !== undefined && pageSize !== undefined) {
      params = params.set('PageNumber', pageNumber.toString());
      params = params.set('PageSize', pageSize.toString());
    }
    if(weiwai){
      params = params.set('OutsourcingStatus', weiwai);
    }
    if(IsWithPart){
      params = params.set('IsWithPart', IsWithPart);
    }
    return this.http.get(environment.apiUrl+ '/JobOrder/GetJobOrdersProcess',{
      params: params,
      headers: new HttpHeaders({
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      })
    })
  }

  GetReportProcessHistoriesByFilter(filter: {
    EmployeeCodeName?: string,
    ProductionNum?: string,
    ProductionName?: string,
    JobOrderNum?: string,
    StartTime?: string,
    EndTime?: string,
    ProcessNum?: string,
    ProcessName?: string,
    PageNumber?: number,
    PageSize?: number
  }) {
    const params = new HttpParams({ fromObject: filter });
    return this.http.get(environment.apiUrl + '/JobOrder/GetReportProcessHistoriesByFilter', {
      params: params,
      headers: new HttpHeaders({
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      })
    });
  }
  AuditProductionWarehousing(id:any){
    const url = this.jobOrder + '/AuditProductionWarehousing';
    let params = new HttpParams().set('id', id);
    return this.http.put(environment.apiUrl+ url,null,{params: params});
  }
  CompleteOrReopenJobOrder(jobOrderId:any,isCompleted:any){
    const url = this.jobOrder + '/CompleteOrReopenJobOrder';
    let params = new HttpParams().set('jobOrderId', jobOrderId).set('isCompleted', isCompleted);
    return this.http.put(environment.apiUrl+ url,null,{params: params});
  }
  CreateJobOrdersBySalesOrderMaterialLineIds(data:any){
    const url = this.jobOrder + '/CreateJobOrdersBySalesOrderMaterialLineIds';
    return this.http.post(environment.apiUrl+ url,data);
  }
  GetJobOrdersProcess3(filter:{
    PartNum?:string,
    PartName?:string,
    JobOrderNum?:string,
    JobOrderStatus?:string,
    JobOrderProcessStatus?:string,
    JobOrderType?:string,
    StartTime?:string,
    EndTime?:string,
    ProcessNum?:string,
    ProcessName?:string,
    OutsourcingStatus?:string,
    PageNumber?:number,
    PageSize?:number,
    IsWithPart?:boolean
    ZhuanHuanWanDe?:boolean
  }){
    const params = new HttpParams({fromObject:filter});
    return this.http.get(environment.apiUrl+ '/JobOrder/GetJobOrdersProcess',{
      params: params
    })
  }
  GetReportingPassRate(){
    return this.http.get(environment.apiUrl+ '/JobOrder/GetReportingPassRate');
  }
  GetAnnualJobOrderProcessCount(){
    const currentYear = new Date().getFullYear();
    return this.http.get(`${environment.apiUrl}/JobOrder/GetAnnualJobOrderProcessCount?year=${currentYear}`);
  }
  GetProductionJobOrderCountAndJobOrderProcessCountAlongWithGrowthRate(){
    return this.http.get(`${environment.apiUrl}/JobOrder/GetProductionJobOrderCountAndJobOrderProcessCountAlongWithGrowthRate`);
  }
  GetProductionTaskToDoList(){
    return this.http.get(`${environment.apiUrl}/JobOrder/GetProductionTaskToDoList`);
  }
  GetTotalProduction(){
    return this.http.get(`${environment.apiUrl}/JobOrder/GetTotalProduction`);
  }
  GetUserJobOrderProcessesByUserCode(employeeCode:any,statusFilter:any){
    const url = this.jobOrder + '/GetUserJobOrderProcessesByUserCode';
    return this.http.get(environment.apiUrl+ url,{params:{employeeCode,statusFilter}});
  }
  GetJobOrderProcessByID(jobOrderProcessId:any){
    const url = this.jobOrder + '/GetJobOrderProcess';
    return this.http.get(environment.apiUrl+ url,{params:{jobOrderProcessId}});
  }
  GetPagedInspectionProject(InspectionProjectName:any,PageNumber:any,PageSize:any){
    const url = this.jobOrder + '/GetPagedInspectionProject';
    return this.http.get(environment.apiUrl+ url,{params:{InspectionProjectName,PageNumber,PageSize}});
  }

  GetInspectionProjectById(id:any){
    const url = this.jobOrder + '/GetInspectionProjectById';
    return this.http.get(environment.apiUrl+ url,{params:{id}});
  }
  DeleteInspectionProject(id:any){
    const url = this.jobOrder + '/DeleteInspectionProject';
    return this.http.delete(environment.apiUrl+ url,{params:{id}});
  }
  CreateInspectionProject(data:any){
    const url = this.jobOrder + '/CreateInspectionProject';
    return this.http.post(environment.apiUrl+ url,data);
  }
  UpdateInspectionProject(id:any,data:any){
    const url = this.jobOrder + '/UpdateInspectionProject';
    return this.http.put(environment.apiUrl+ url,data,{params:{id}});
  }

  PerformProductionInspection(data:any){
    const url = this.jobOrder + '/PerformProductionInspection';
    return this.http.put(environment.apiUrl+ url,data);
  }

  GetPagedProductionInspectionOrder(
    filter:{
      InspectionNum?:string,
      JobOrderNum?:string,
      ProductNum?:string,
      Inspector?:string,
      PageNumber?:number,
      PageSize?:number
    }
  ){
    const url = this.jobOrder + '/GetPagedProductionInspectionOrder';
    return this.http.get(environment.apiUrl+ url,{params:filter});
  }
  GetProductionInspectionOrderById(id:any){
    const url = this.jobOrder + '/GetProductionInspectionOrderById';
    return this.http.get(environment.apiUrl+ url,{params:{id}});
  }
  CreateProductionInspectionOrder(data:any){
    const url = this.jobOrder + '/CreateProductionInspectionOrder';
    return this.http.post(environment.apiUrl+ url,data);
  }
}


